<template>
  <div>
    <basic-dialog
      :icon="icon"
      :title="title"
      width="850px"
      height="85%"
      v-bind="$attrs"
      @close="close"
      :openFlag="openEdit"
    >
      <template #content>
        <el-form label-width="140px" label-position="right" :model="form" :rules="rules" ref="formRef" :disabled="true">
          <el-row>
            <el-col span="12">
              <el-form-item label="物料名称" prop="tagNumber">
                <el-input placeholder="物料名称" clearable v-model="form.tagNumber" :style="`width:${width}` "
                          :size="size" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="物料类型">
                <el-select placeholder="物料类型" clearable v-model="form.materialType" :style="`width:${width}`"
                           :size="size">
                  <el-option label="测试部门" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="统一编码">
                <el-input placeholder="统一编码" clearable v-model="form.unifiedCoding" :style="`width:${width}` "
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="CAS号">
                <el-input placeholder="CAS号" clearable v-model="form.casNumber" style="width:100%;"
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="是否为危化品">
                <el-radio-group v-model="form.danChemicals">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="危险化学品目录序号">
                <el-input placeholder="危险化学品目录序号" clearable v-model="form.catalogueNo" style="width:100%;"
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="危化品类别" prop="category">
                <el-select placeholder="危化品类别" clearable v-model="form.category" :style="`width:${width}`"
                           :size="size">
                  <el-option label="测试部门" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="物质形态">
                <el-select placeholder="物质形态" clearable v-model="form.materialForm" :style="`width:${width}`"
                           :size="size">
                  <el-option label="测试部门" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="年消耗量（吨）" prop="annualConsumption">
                <el-input placeholder="年消耗量（吨）" clearable v-model="form.annualConsumption" style="width:100%;"
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="最大储量（吨）">
                <el-input placeholder="最大储量（吨）" v-model.number="form.maxStorageCapacity" :style="`width:${width}` " :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="实际储量（吨）" prop="actualReserves">
                <el-input placeholder="实际储量（吨）" clearable v-model.number="form.actualReserves" style="width:100%;"
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="单位">
                <el-input placeholder="单位" v-model="form.measureUnit" :style="`width:${width}` " :size="size" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="储存位置" prop="storageLocation">
                <el-input placeholder="储存位置" clearable v-model="form.storageLocation" style="width:100%;"
                          :size="size"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
              <el-form-item label="储存方式">
                <el-select placeholder="储存方式" clearable v-model="form.storageMode" :style="`width:${width}`"
                           :size="size">
                  <el-option label="测试部门" :value="1"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="所属工艺流程" prop="processFlow">
                <el-input placeholder="所属工艺流程" clearable v-model="form.processFlow" style="width:100%;"
                          :size="size" :disabled="true"></el-input>
              </el-form-item>
            </el-col>
            <el-col span="12">
                <el-form-item label="是否为剧毒化学品">
                  <el-radio-group v-model="form.danChemicals">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="是否重点监管危险化学品">
                  <el-radio-group v-model="form.focus">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>      
            </el-col>
            <el-col span="12">
                <el-form-item label="是否特别管控危险化学品">
                  <el-radio-group v-model="form.specialControl">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col span="12">
              <el-form-item label="是否易制毒">
                  <el-radio-group v-model="form.easyMakeDrugs">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>      
            </el-col>
            <el-col span="12">
                <el-form-item label="是否易制爆">
                  <el-radio-group v-model="form.easyExplode">
                      <!-- <el-radio 
                      v-for="(item,index) in sexData"
                      :key="index"
                      :label="item.dictValue"
                      >{{item.dictValueName}}</el-radio> -->
                      <el-radio :label="0">是</el-radio>
                      <el-radio :label="1">否</el-radio>

                    </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="安全措施">
                <el-input placeholder="安全措施" clearable v-model="form.safetyMeasures" type="textarea" :rows="4"
                          style="width: 98.5%" :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="应急处理措施">
                <el-input placeholder="应急处理措施" clearable v-model="form.emergencyMeasures" type="textarea" :rows="4"
                          style="width: 98.5%" :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>          
          <el-row>
            <el-col>
              <el-form-item label="备注">
                <el-input placeholder="备注" clearable v-model="form.remark" style="width: 98.5%" :size="size"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <template #bottom>
        <div>
          <el-button type="primary" @click="submitForm('form')"
          >保存
          </el-button
          >
          <el-button type="success" @click="reset('form')"
          >重置
          </el-button
          >
          <el-button type="warning" @click="close">取消</el-button>
        </div>
      </template>
    </basic-dialog>
  </div>
</template>
<script>
import {materialsCreateAPI,materialsViewAPI,materialsUpdateAPI} from '@/api/enterprise/materials/index';

export default {
  name: "editDialog",
  data() {
    return {
      icon: 'icon-bianji',
      title: '新增物料信息',
      openEdit: false,
      width: "260px",
      size: "small",
      form: {},
      rules: {
        // 有限空间名称
        restrictSpaceName: [
          {required: true, message: "请输入有限空间", trigger: "blur"},
        ],
        // 易导致事故类型
        accidentType: [
          {required: true, message: "请选择易导致事故类型", trigger: "change"},
        ]
      },
      rowData: {}
    }
  },
  props: {
    dictConfig: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  methods: {
    // 打开
    open(row) {
      
      this.openEdit = true
      if (row && row.id) {
        this.rowData = row;
        this.getDetail(row.id)
      }
    },
    // 关闭
    close() {
      this.openEdit = false
      this.reset()
    },
    // 重置
    reset() {
      this.$refs.formRef.resetFields()
      this.form = {}
    },
    // 提交接口
    submitForm() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          if (this.rowData.id) this.modify()
          else this.add()
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 新增
    async add() {
      try {
        const res = await materialsCreateAPI(this.form);
        if (res.code === 200) {
          this.close()
          this.$emit('refresh')
        }
      } catch (e) {
        this.$message.error(e)
      }
    },
    // 修改
    async modify() {
      try {
        const res = await materialsUpdateAPI({...this.form, id: this.rowData.id});
        if (res.code === 200) {
          this.close()
          this.$emit('refresh')
        }
      } catch (e) {
        this.$message.error(e)
      }
    },
    // 上传图片成功
    onSuccess(fileList, params) {
      this.form[params] = fileList
    },
    // 删除图片
    onRemove(fileList, params) {
      this.form[params] = fileList
    },
    // 获取详情
    async getDetail(id) {
      try {
        const res = await materialsViewAPI({id})
        if (res.code === 200) {
          this.form ={... res.data}
          // 为图片添加名字
          this.form.file=res.data.file.map(item=>{
            return{
              ...item,
              name:item.originalFilename
            }
          })
          console.log(this.form)
        }
      } catch (e) {
        this.$message.error(e)
      }
    }
  }
}
</script>

<style scoped lang="scss">
/deep/ .el-form-item {
  margin-bottom: 8px !important;
}

/deep/ .el-form-item__error {
  line-height: 2px;
}

.fujian{
  display: flex;
  border-bottom: 1px solid #4A80FC;
      margin: 8px;
    margin-bottom: 18px;
  .fujianOne{
    width: 2px;
        height: 20px;
    margin-top: 3px;
    background: #4A80FC;
    border-radius: 1px 1px 1px 1px;
    opacity: 1;
  }
  .fujianTwo{
        margin-left: 7px;
    font-size: 16px;
    margin-bottom: 3px;
  }
}

</style>
